React Js / Components / Data sharing between components - basics
Data sharing
-
1. Step
1. Types of Component Relationships
1. Parent → Child ✅ (easy: use props)
2. Child → Parent ✅ (via function passed as prop)
3. Sibling → Sibling ⚠️ (via common parent)
4. Unrelated components 🧠 (use Context API, Redux, Zustand, etc.)
1. Parent to Child (Using Props)
// Parent.jsx import Child from './Child'; function Parent() { const message = "Hello from Parent!"; return ; } // Child.jsx function Child({ message }) { return {message}
; }2. Child to Parent (Using Callback/function Props)
// Parent.jsx import Child from './Child'; import { useState } from 'react'; function Parent() { const [childData, setChildData] = useState(''); const handleDataFromChild = (data) => { setChildData(data); }; return ( ); }Data from child: {childData}
// Child.jsx function Child({ sendData }) { return ( ); } 3. Sibling to Sibling (via Parent)
// Parent.jsx import Sibling1 from './Sibling1'; import Sibling2 from './Sibling2'; import { useState } from 'react'; function Parent() { const [data, setData] = useState(''); return ( <> > ); } // Sibling1.jsx function Sibling1({ setData }) { return ( ); } // Sibling2.jsx function Sibling2({ data }) { return Received in Sibling 2: {data}
; }
MANVIA BLOG